package com.dirtylily.myblog.service;

import com.dirtylily.myblog.dao.BlogDao;
import com.dirtylily.myblog.dao.TagDao;
import com.dirtylily.myblog.pojo.*;
import com.dirtylily.myblog.utils.markDownUtils;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class BlogService {
    @Autowired
    BlogDao blogDao ;

    public blog getBlog(Long id){

        try {


            blog blog = blogDao.getBlog(id);
            return blog;

        }
        catch (Exception e){
            System.out.println("博客查找失败");
        }
        return null;
    }


    public List<BlogQuery> getAllBlogQuery() {
        return blogDao.getAllBlogQuery();
    }

    public int updateBlog(ShowBlog sb){

        sb.setUpdateTime(new Date());
        int i = blogDao.updateBlog(sb);

        return i;
    }

    public List<blog> getAllBlog() {
        return blogDao.getAllBlog();
    }
    public List<blog> getAllBlogByTypeId(Long id) {
        return blogDao.getAllBlogByTypeId(id);
    }

    public List<blog> getAllBlogByTagId(Long id) {
        return blogDao.getAllBlogByTagId(id);
    }

    public List<blog> getAllBlogByUpdateTime(Integer num) {

        ArrayList<blog> blogs = new ArrayList<>();
        List<blog> allBlogByUpdateTime = blogDao.getAllBlogByUpdateTime(0);
        if(num==0||num>=allBlogByUpdateTime.size()){
            return allBlogByUpdateTime;
        }
        for(int i =0;i<num;i++){
            blogs.add(allBlogByUpdateTime.get(i));

        }
        return blogs;
    }
    public List<blog> getAllRecomendedBlog(Integer num) {
        List<blog> allBlogByUpdateTime = blogDao.getAllBlogByUpdateTime(0);

        ArrayList<blog> returnBlogs = new ArrayList<>();
        if(num==0||num>=allBlogByUpdateTime.size()){
            for (blog blog : allBlogByUpdateTime) {
                if(blog.isRecommend()){
                    returnBlogs.add(blog);
                }
            }
        }
        else {
            for (int i = 0; ; i++) {
                if (returnBlogs.size() == num) {
                    break;
                }
                blog blog = allBlogByUpdateTime.get(i);
                if (blog.isRecommend()) {
                    returnBlogs.add(blog);
                }

            }
        }
        return returnBlogs;
    }

    public ShowBlog getShowBlog(Long id) {
        return blogDao.getShowBlog(id);
    }


    public int saveBlog(blog blog){
        if(blog.getId()==null){
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);}
        else {
            blog.setUpdateTime(new Date());
        }
        int i = blogDao.saveBlog(blog);
        return i;
    }
    public blog findBlogAndMarkdownToHtml(Long id) {
        blog blog = blogDao.getBlog(id);

        /**
         * new一个新的blog，把数据库的blog拷贝到新的blog中
         * 用新的blog操作转换
         * 此操作不影响数据库查出来的blog值
         */
        blog b = new blog();
        BeanUtils.copyProperties(blog, b);
        String content = b.getContent();
        String htmlContent = markDownUtils.markdownToHtmlExtensions(content);
        b.setContent(htmlContent);
        blogDao.updateViews(id);
        //更新浏览次数
        return b;
    }


    public int delBlog(Long id){
        int i = blogDao.delBlog(id);
        return i;
    }

    public List<BlogQuery> getBlogBySearch(SearchBlog searchBlog) {

        return blogDao.searchByTitleAndType(searchBlog);
    }
    public List<blog> getBlogByTitle(String title){
        return blogDao.getBlogByTitle(title);
    }


    public Map<String, List<blog>> archiveBlog() {
        List<String> years = blogDao.findGroupYear();
        Map<String, List<blog>> map = new TreeMap<>(Collections.reverseOrder());
        for (String year : years) {
            map.put(year, blogDao.getAllBlogByUpdateYear(year));
        }

        return map;
    }

    public Long countBlog() {
        return blogDao.count();
    }

}

